package com.wencai.util;

import com.wencai.bean.NativeStock;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangpan
 * @date 2023/4/16
 */
public class Import {
    private static String dir = "C:\\Users\\Administrator\\Downloads\\概念板块\\";
    private static String file = "4月14日概念板块指数涨幅大于4%涨幅从大到小排序....xlsx";

    public static void main(String[] args) {
        Map<String, List<NativeStock>> data = getData(dir + file);
        System.out.println(data);
    }

    public static Map<String, List<NativeStock>> getData(String file) {
        ExcelListener listener = new ExcelListener();
        ExcelReader reader = EasyExcel.read(file, listener).build();
        List<ReadSheet> readSheets = reader.excelExecutor().sheetList();

        String date = null;
        List<NativeStock> dataList = new ArrayList<>();
        for (ReadSheet readSheet : readSheets) {
            reader.read(readSheet);
            List<Object> list = listener.getList();
            int i = 0;
            for (Object o : list) {
                Map map = (Map) o;
                if (i++ == 0) {
                    date = map.get(11).toString().replaceAll(".*?\\s*(\\d{4}\\.\\d{2}\\.\\d{2})", "$1");
                    continue;
                }
                String stockName = map.get(1).toString();
                String up = map.get(4).toString();
                String obj;
                if (up.matches("\\d+\\.\\d+")) {
                    obj = up;
                } else {
                    obj = map.get(3).toString();
                }
                if (!stockName.equals("undefined")) {
                    NativeStock nativeStock = new NativeStock(stockName, Double.valueOf(obj));
                    dataList.add(nativeStock);
                }
            }
        }
        reader.finish();
        Map<String, List<NativeStock>> map = new HashMap<>();
        map.put(date, dataList);
        return map;
    }
}
